/*
 * Splitter.h
 *
 *  Created on: Jan 26, 2012
 *      Author: jiabin
 */

#ifndef SPLITTER_H_
#define SPLITTER_H_

#include "Host.h"
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <string>

class Splitter
{
private:
	Host client;
	std::vector<Host> agents;

	int clientfd;
	std::vector<int> agentsfd;

	static const unsigned short listenPort = 10000;
	static const size_t buflen = 1024;

public:
	Splitter(const Host& client, const std::vector<Host>& agents);

	void run();

	virtual ~Splitter();

private:
	void initClient();

	void initAgents();

	int initUDPAgent(const Host& agent);

	void relay(char *buf, int length, int index);

	static inline void panic(std::string s)
	{
		perror(s.c_str());
		exit(1);
	}
};

#endif /* SPLITTER_H_ */
